package demo.filter;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

/**
 * 
 * 判断用户是否登录
 *
 */
public class FilterDemo implements Filter {
    private static final Logger logger = LoggerFactory.getLogger(FilterDemo.class);

    private WebApplicationContext context;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        String tmp = Arrays.toString(beanDefinitionNames);
        logger.info("所有的Bean定义BeanDefinitionNames:{}", tmp);
        chain.doFilter(request, response);
        logger.info("所有的Bean定义BeanDefinitionNames:{}", tmp);
    }

    @Override
    public void init(FilterConfig filterConfig) {
        logger.info("初始化FilterDemo");
        this.context = (WebApplicationContext) filterConfig.getServletContext()
                .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    }

    @Override
    public void destroy() {
        logger.info("销毁FilterDemo");
    }

}